]> git.saurik.com Git - bison.git/blame - data/c++.m4
lalr1.cc: use state_type.
[bison.git] / data / c++.m4
CommitLineData
08af01c2 1 -*- Autoconf -*-
cf147260
AD
2
3# C++ skeleton for Bison
4
b47b6ff7
AD
5# Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
6# Free Software Foundation, Inc.
cf147260 7
f16b0819 8# This program is free software: you can redistribute it and/or modify
cf147260 9# it under the terms of the GNU General Public License as published by
f16b0819 10# the Free Software Foundation, either version 3 of the License, or
cf147260 11# (at your option) any later version.
f16b0819 12#
cf147260
AD
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16# GNU General Public License for more details.
f16b0819 17#
cf147260 18# You should have received a copy of the GNU General Public License
f16b0819 19# along with this program. If not, see <http://www.gnu.org/licenses/>.
cf147260 20
3953ed88
AD
21m4_include(b4_pkgdatadir/[c.m4])
22
cf147260
AD
23## ---------------- ##
24## Default values. ##
25## ---------------- ##
26
a4e25e1d
JD
27b4_percent_define_default([[parser_class_name]], [[parser]])
28b4_percent_define_default([[location_type]], [[location]])
29b4_percent_define_default([[filename_type]], [[std::string]])
67501061
AD
30
31# api.namespace defaults to namespace, and then to b4_prefix.
32b4_percent_define_ifdef([api.namespace],
33 [],
34 [b4_percent_define_ifdef([namespace],
35 [b4_percent_define_copy([namespace], [api.namespace])],
36 [b4_percent_define_default([api.namespace],
37 m4_defn([b4_prefix]))])])
38
c1d19e10 39b4_percent_define_default([[global_tokens_and_yystype]], [[false]])
a4e25e1d
JD
40b4_percent_define_default([[define_location_comparison]],
41 [m4_if(b4_percent_define_get([[filename_type]]),
c1d19e10 42 [std::string], [[true]], [[false]])])
cf147260
AD
43
44
67501061 45
793fbca5
JD
46## ----------- ##
47## Namespace. ##
48## ----------- ##
49
67501061
AD
50m4_define([b4_namespace_ref], [b4_percent_define_get([[api.namespace]])])
51
793fbca5
JD
52
53# Don't permit an empty b4_namespace_ref. Any `::parser::foo' appended to it
54# would compile as an absolute reference with `parser' in the global namespace.
55# b4_namespace_open would open an anonymous namespace and thus establish
56# internal linkage. This would compile. However, it's cryptic, and internal
57# linkage for the parser would be specified in all translation units that
58# include the header, which is always generated. If we ever need to permit
59# internal linkage somehow, surely we can find a cleaner approach.
60m4_if(m4_bregexp(b4_namespace_ref, [^[ ]*$]), [-1], [],
67501061 61[b4_complain_at(b4_percent_define_get_loc([[api.namespace]]),
793fbca5
JD
62 [[namespace reference is empty]])])
63
64# Instead of assuming the C++ compiler will do it, Bison should reject any
65# invalid b4_namepsace_ref that would be converted to a valid
66# b4_namespace_open. The problem is that Bison doesn't always output
67# b4_namespace_ref to uncommented code but should reserve the ability to do so
68# in future releases without risking breaking any existing user grammars.
69# Specifically, don't allow empty names as b4_namespace_open would just convert
70# those into anonymous namespaces, and that might tempt some users.
71m4_if(m4_bregexp(b4_namespace_ref, [::[ ]*::]), [-1], [],
67501061 72[b4_complain_at(b4_percent_define_get_loc([[api.namespace]]),
793fbca5
JD
73 [[namespace reference has consecutive "::"]])])
74m4_if(m4_bregexp(b4_namespace_ref, [::[ ]*$]), [-1], [],
67501061 75[b4_complain_at(b4_percent_define_get_loc([[api.namespace]]),
793fbca5
JD
76 [[namespace reference has a trailing "::"]])])
77
78m4_define([b4_namespace_open],
67501061 79[b4_user_code([b4_percent_define_get_syncline([[api.namespace]])
793fbca5
JD
80[namespace ]m4_bpatsubst(m4_dquote(m4_bpatsubst(m4_dquote(b4_namespace_ref),
81 [^\(.\)[ ]*::], [\1])),
82 [::], [ { namespace ])[ {]])])
83
84m4_define([b4_namespace_close],
67501061 85[b4_user_code([b4_percent_define_get_syncline([[api.namespace]])
4977e0a7 86m4_bpatsubst(m4_dquote(m4_bpatsubst(m4_dquote(b4_namespace_ref[ ]),
b987342b 87 [^\(.\)[ ]*\(::\)?\([^][:]\|:[^:]\)*],
793fbca5 88 [\1])),
b987342b 89 [::\([^][:]\|:[^:]\)*], [} ])[} // ]b4_namespace_ref])])
793fbca5
JD
90
91
cf147260
AD
92# b4_token_enums(LIST-OF-PAIRS-TOKEN-NAME-TOKEN-NUMBER)
93# -----------------------------------------------------
c095d689 94# Output the definition of the tokens as enums.
cf147260 95m4_define([b4_token_enums],
cf147260
AD
96[/* Tokens. */
97 enum yytokentype {
98m4_map_sep([ b4_token_enum], [,
99],
100 [$@])
101 };
c095d689 102])
cf147260
AD
103
104
105## ----------------- ##
106## Semantic Values. ##
107## ----------------- ##
108
b9e4eb5b
AD
109# b4_semantic_type_declare
110# ------------------------
111# Declare semantic_type.
112m4_define([b4_semantic_type_declare],
113[ /// Symbol semantic values.
114m4_ifdef([b4_stype],
115[ union semantic_type
116 {b4_user_stype
117 };],
118[m4_if(b4_tag_seen_flag, 0,
119[[ typedef int semantic_type;]],
120[[ typedef YYSTYPE semantic_type;]])])])
121
122
4f84717d
AD
123# b4_public_types_declare
124# -----------------------
125# Define the public types: token, semantic value, location, and so forth.
1d6b689b 126# Depending on %define token_lex, may be output in the header or source file.
4f84717d
AD
127m4_define([b4_public_types_declare],
128[[#ifndef YYSTYPE
129]b4_semantic_type_declare[
130#else
131 typedef YYSTYPE semantic_type;
132#endif]b4_locations_if([
133 /// Symbol locations.
134 typedef b4_percent_define_get([[location_type]]) location_type;])[
1d6b689b 135
4f84717d
AD
136 /// Tokens.
137 struct token
138 {
139 ]b4_token_enums(b4_tokens)[
140 };
1d6b689b 141
4f84717d
AD
142 /// Token type.
143 typedef token::yytokentype token_type;
1d6b689b
AD
144
145 /// A complete symbol, with its type.
146 template <typename Exact>
147 struct symbol_base_type
148 {
149 /// Default constructor.
150 inline symbol_base_type ();
151
152 /// Constructor.]b4_locations_if([
153 inline symbol_base_type (const location_type& l)])[;
154 inline symbol_base_type (]b4_args(
155 [const semantic_type& v],
156 b4_locations_if([const location_type& l]))[);
157
158 /// Return this with its exact type.
159 const Exact& self () const;
160 Exact& self ();
161
162 /// Return the type of this symbol.
163 int type_get () const;
164
165 /// The semantic value.
166 semantic_type value;]b4_locations_if([
167
168 /// The location.
169 location_type location;])[
170 };
171
172 /// External form of a symbol: its type and attributes.
173 struct symbol_type : symbol_base_type<symbol_type>
174 {
175 /// The parent class.
176 typedef symbol_base_type<symbol_type> super_type;
177
178 /// Default constructor.
179 inline symbol_type ();
180
181 /// Constructor.
182 inline symbol_type (]b4_args([int t],
183 [const semantic_type& v],
184 b4_locations_if([const location_type& l]))[);
185
186 inline symbol_type (]b4_args([int t],
187 b4_locations_if([const location_type& l]))[);
188
189 /// The symbol type.
190 int type;
191
192 /// Return the type corresponding to this state.
193 inline int type_get_ () const;
194
195 /// Its token.
196 inline token_type token () const;
197 };
0623bacc 198]b4_symbol_constructor_declare])
4f84717d
AD
199
200
1d6b689b
AD
201# b4_public_types_define
202# ----------------------
203# Provide the implementation needed by the public types.
204m4_define([b4_public_types_define],
205[[ // symbol_base_type.
206 template <typename Exact>
207 ]b4_parser_class_name[::symbol_base_type<Exact>::symbol_base_type ()
208 : value()]b4_locations_if([
209 , location()])[
210 {
211 }]b4_locations_if([[
212
213 template <typename Exact>
214 ]b4_parser_class_name[::symbol_base_type<Exact>::symbol_base_type (const location_type& l)
215 : value()
216 , location(l)
217 {
218 }]])[
219
220 template <typename Exact>
221 ]b4_parser_class_name[::symbol_base_type<Exact>::symbol_base_type (]b4_args(
222 [const semantic_type& v],
223 b4_locations_if([const location_type& l]))[)
224 : value(v)]b4_locations_if([
225 , location(l)])[
226 {
227 }
228
229 template <typename Exact>
230 const Exact&
231 ]b4_parser_class_name[::symbol_base_type<Exact>::self () const
232 {
233 return static_cast<const Exact&>(*this);
234 }
235
236 template <typename Exact>
237 Exact&
238 ]b4_parser_class_name[::symbol_base_type<Exact>::self ()
239 {
240 return static_cast<Exact&>(*this);
241 }
242
243 template <typename Exact>
244 int
245 ]b4_parser_class_name[::symbol_base_type<Exact>::type_get () const
246 {
247 return self ().type_get_ ();
248 }
249
250 // symbol_type.
251 ]b4_parser_class_name[::symbol_type::symbol_type ()
252 : super_type ()
253 , type ()
254 {
255 }
256
257 ]b4_parser_class_name[::symbol_type::symbol_type (]b4_args(
258 [int t],
259 b4_locations_if([const location_type& l]))[)
260 : super_type (]b4_locations_if([l])[)
261 , type (t)
262 {
263 }
264
265 ]b4_parser_class_name[::symbol_type::symbol_type (]b4_args(
266 [int t],
267 [const semantic_type& v],
268 b4_locations_if([const location_type& l]))[)
269 : super_type (v]b4_locations_if([, l])[)
270 , type (t)
271 {
272 }
273
274 int
275 ]b4_parser_class_name[::symbol_type::type_get_ () const
276 {
277 return type;
278 }
279]b4_lex_symbol_if([[
280 ]b4_parser_class_name[::token_type
281 ]b4_parser_class_name[::symbol_type::token () const
282 {
283 // YYTOKNUM[NUM] -- (External) token number corresponding to the
284 // (internal) symbol number NUM (which must be that of a token). */
285 static
286 const ]b4_int_type_for([b4_toknum])[
287 yytoken_number_[] =
288 {
289 ]b4_toknum[
290 };
291 return static_cast<token_type> (yytoken_number_[type]);
292 }
0623bacc
AD
293]])[]dnl
294b4_symbol_constructor_define])
295
296
297# b4_symbol_constructor_declare
298# b4_symbol_constructor_define
299# -----------------------------
300# Declare/define symbol constructors for all the value types.
301# Use at class-level. Redefined in variant.hh.
302m4_define([b4_symbol_constructor_declare], [])
303m4_define([b4_symbol_constructor_define], [])
304
1d6b689b 305
cbf25ce7
AD
306# b4_yytranslate_define
307# ---------------------
308# Define yytranslate_. Sometimes used in the header file,
309# sometimes in the cc file.
310m4_define([b4_yytranslate_define],
311[[ // Symbol number corresponding to token number t.
312 ]b4_parser_class_name[::token_number_type
313 ]b4_parser_class_name[::yytranslate_ (]b4_lex_symbol_if([token_type],
314 [int])[ t)
315 {
316 static
317 const token_number_type
318 translate_table[] =
319 {
320]b4_translate[
321 };
322 const unsigned int user_token_number_max_ = ]b4_user_token_number_max[;
323 const token_number_type undef_token_ = ]b4_undef_token_number[;
324
325 if (static_cast<int>(t) <= yyeof_)
326 return yyeof_;
327 else if (static_cast<unsigned int> (t) <= user_token_number_max_)
328 return translate_table[t];
329 else
330 return undef_token_;
331 }
332]])
333
1d6b689b 334
cf147260
AD
335# b4_lhs_value([TYPE])
336# --------------------
337# Expansion of $<TYPE>$.
338m4_define([b4_lhs_value],
1fa5d8bb 339[b4_symbol_value([yyval], [$1])])
cf147260
AD
340
341
342# b4_rhs_value(RULE-LENGTH, NUM, [TYPE])
343# --------------------------------------
344# Expansion of $<TYPE>NUM, where the current rule has RULE-LENGTH
345# symbols on RHS.
346m4_define([b4_rhs_value],
1fa5d8bb
AD
347[b4_symbol_value([yysemantic_stack_@{($1) - ($2)@}], [$3])])
348
cf147260
AD
349
350# b4_lhs_location()
351# -----------------
352# Expansion of @$.
353m4_define([b4_lhs_location],
354[(yyloc)])
355
356
357# b4_rhs_location(RULE-LENGTH, NUM)
358# ---------------------------------
359# Expansion of @NUM, where the current rule has RULE-LENGTH symbols
360# on RHS.
361m4_define([b4_rhs_location],
d1ff7a7c 362[(yylocation_stack_@{($1) - ($2)@})])
cf147260
AD
363
364
365# b4_parse_param_decl
366# -------------------
367# Extra formal arguments of the constructor.
368# Change the parameter names from "foo" into "foo_yyarg", so that
369# there is no collision bw the user chosen attribute name, and the
370# argument name in the constructor.
371m4_define([b4_parse_param_decl],
372[m4_ifset([b4_parse_param],
373 [m4_map_sep([b4_parse_param_decl_1], [, ], [b4_parse_param])])])
374
375m4_define([b4_parse_param_decl_1],
376[$1_yyarg])
377
378
379
380# b4_parse_param_cons
381# -------------------
382# Extra initialisations of the constructor.
383m4_define([b4_parse_param_cons],
384 [m4_ifset([b4_parse_param],
fa7b79c0 385 [
cf147260
AD
386 b4_cc_constructor_calls(b4_parse_param)])])
387m4_define([b4_cc_constructor_calls],
388 [m4_map_sep([b4_cc_constructor_call], [,
389 ], [$@])])
390m4_define([b4_cc_constructor_call],
391 [$2 ($2_yyarg)])
392
393# b4_parse_param_vars
394# -------------------
395# Extra instance variables.
396m4_define([b4_parse_param_vars],
397 [m4_ifset([b4_parse_param],
398 [
399 /* User arguments. */
400b4_cc_var_decls(b4_parse_param)])])
401m4_define([b4_cc_var_decls],
402 [m4_map_sep([b4_cc_var_decl], [
403], [$@])])
404m4_define([b4_cc_var_decl],
405 [ $1;])